/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.unavarra.mti.iws.model;

import es.unavarra.mti.iws.logic.editor.Rotable;
import es.unavarra.mti.iws.logic.editor.Selectable;
import es.unavarra.mti.iws.logic.properties.HasProperties;
import es.unavarra.mti.iws.logic.properties.KProperty;
import es.unavarra.mti.iws.utils.ReflectionUtils;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Marcelo
 */
public class Link implements Selectable,Rotable,HasProperties{

    private Node nodeSource;
    private Node nodeTarget;

    public Link() {
    }

    public Node getNodeSource() {
        return nodeSource;
    }

    public void setNodeSource(Node nodeSource) {
        this.nodeSource = nodeSource;
    }

    public Node getNodeTarget() {
        return nodeTarget;
    }

    public void setNodeTarget(Node nodeTarget) {
        this.nodeTarget = nodeTarget;
    }

    public float getDistance() {
        return getSizeY();
    }

    public void setDistance(float distance) {

    }



    public float getX() {
        if (nodeSource == null || nodeTarget == null) {return 0.0f;}
        return (nodeSource.getX() + nodeTarget.getX()) / 2.0f;
    }

    public float getY() {
        if (nodeSource == null || nodeTarget == null) {return 0.0f;}
        return (nodeSource.getY() + nodeTarget.getY()) / 2.0f;
    }

    public float getSizeX() {
        return 16.0f;
    }

    public float getSizeY() {
        if (nodeSource == null || nodeTarget == null) {return 0.0f;}
        float dx = nodeSource.getX() - nodeTarget.getX();
        float dy = nodeSource.getY() - nodeTarget.getY();
        return (float)Math.sqrt(dx*dx+dy*dy);
    }

    public float getRotation() {
        if (nodeSource == null || nodeTarget == null) {return 0.0f;}
        float dx = nodeSource.getX() - nodeTarget.getX();
        float dy = nodeSource.getY() - nodeTarget.getY();
        return (float)(Math.atan2(dx, dy) * 180.0 / Math.PI);
    }

    public void setPosition(float x, float y) {}
    public void setRotation(float r) {}


    @Override
    public List<KProperty> objectProperties() {
        List<KProperty> prop = new ArrayList<KProperty>();
        insertMyProperties(prop);
        return prop;
    }


    @Override
    public List<KProperty> objectProperties(Class<? extends HasProperties> c) {
        if (Link.class.isAssignableFrom( c )){
            List<KProperty> prop = new ArrayList<KProperty>();
            insertMyProperties(prop);
            return prop;
        }
        return new ArrayList<KProperty>();
    }

    private void insertMyProperties(List<KProperty> prop) {
        prop.add(ReflectionUtils.createProperty(Link.class, "distance").setName("distancia"));
    }
}
